package priv.cwr.springboot.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@SpringBootApplication
@EnableAsync
@EnableScheduling
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    /**
     * 用户中心线程池
     *
     * @return
     */
    @Bean
    public Executor userCenter() {
        return getExecutor(50, 150, 100000, "user-center");
    }

    /**
     * 开启异步线程池
     *
     * @param CorePoolSize     核心线程数
     * @param MaxPoolSize      最大线程数
     * @param QueueCapacity    缓存队列大小
     * @param ThreadNamePrefix 线程前缀
     * @return
     */
    private Executor getExecutor(int CorePoolSize, int MaxPoolSize,
                                 int QueueCapacity, String ThreadNamePrefix) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CorePoolSize);
        executor.setMaxPoolSize(MaxPoolSize);
        executor.setQueueCapacity(QueueCapacity);
        executor.setThreadNamePrefix(ThreadNamePrefix);
        executor.initialize();
        return executor;
    }
}
